package com.icss.ah.logistic.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.bean.WxCorpInfoBean;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.logistic.service.LogisticService;
import com.icss.ah.orderList.service.OrderListService;
import com.wx.util.LogUtils;
import com.wx.util.WxUtil;
/**
 * 物流信息
 * @author whr
 */
@SuppressWarnings("serial")
@Controller
public class LogisticAction extends BaseAction{
	@Autowired
	private LogisticService logisticService;
	
	@Autowired
	private OrderListService orderListService;
	private final Logger errlog = Logger.getLogger("E");
	
	/**
	 * @title：物流信息录入  页面
	 * @method：toInputLogisticInfoPage
	 * @return：
	 **/
	@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
	public String toInputLogisticInfoPage(){
		request = this.getRequest();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId = userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		String orderNo = request.getParameter("orderNo")==null?"":request.getParameter("orderNo");
		HashMap map = new HashMap();
		map.put("corpId",corpId);	//公众号id
		map.put("shopId", shopId);	//商铺id
		map.put("orderNo", orderNo);
		
		List<Map> data = new ArrayList<Map>();
		data = logisticService.getOrderByOrderNo("LogisticBean.getOrderByOrderNo", map);
		String productName = "";
		for (int i = 0; i < data.size(); i++) {
			if(i==(data.size()-1)){
				productName += data.get(i).get("PRODUCT_NAME") + "";
			}else{
				productName += data.get(i).get("PRODUCT_NAME") + ",";
			}
		}
		
		request.setAttribute("orderNo", orderNo);
		request.setAttribute("productName", productName);
		return "toInputLogisticInfoPage";
	}
	
	/**
	 * @title：物流信息录入  页面
	 * @method：addLogisticInfo
	 * @return：
	 **/
	public void addLogisticInfo(){
		request = this.getRequest();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId = userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		String orderNo = request.getParameter("orderNo")==null?"":request.getParameter("orderNo");
		String logisticInfo = request.getParameter("logisticInfo")==null?"":request.getParameter("logisticInfo");
		String logisticStatus = request.getParameter("logisticStatus")==null?"":request.getParameter("logisticStatus");	
		String templateId = getTemlateIdByCorp(corpId,"WL_INFO");	//获取templateId
		WxCorpInfoBean corpInfo = getWxCorpInfoBeanByCorpId(corpId);
		String corpSecret = corpInfo.getCorpSecret(); //获取cropSecret(开发者凭据)
		String corpKey = corpInfo.getCorpKey(); //获取cropSecret(开发者凭据)
		String result = "1";
		try {
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("UUID", UUID.randomUUID().toString());
			map.put("SHOP_ID", shopId);
			map.put("CORP_ID", corpId);
			map.put("ORDER_NO", orderNo);
			map.put("LOGISTICS_INTO", logisticInfo);
			map.put("LOGISTICS_STATUS", logisticStatus);
			map.put("templateId", templateId);
			map.put("corpSecret", corpSecret);
			map.put("corpKey", corpKey);
			map.put("TMP_ORDER_URL", TMP_ORDER_URL);
			//写入物流信息
			boolean jg = logisticService.addLogisticInfoNew(map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.info("LogisticAction|addLogisticInfo",e);
			result = "2";
		} finally {
			str2Resp(result);
		}
	}

	
	public void queryStatus() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();		//商铺id
			String corpId=userInfo.getCorpId();   //公众号id 
			String orderNo = request.getParameter("orderNo");
			orderNo = orderNo ==null ? "" : orderNo;

			HashMap map = new HashMap();
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("orderNo",orderNo);
			HashMap statusMap=logisticService.queryStatus(map);
			jsonText.setSuccess(true);
			if(statusMap!=null){
				jsonText.setObj(statusMap);
			}else{
				jsonText.setObj("");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
			jsonText.setMsg(e.getMessage());
			errlog.error("LogisticAction|queryStatus|查询物流状态失败：",e);
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
		
	}
}
